﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

using ScintillaNet;
using Descriptions;

namespace IHMainApp
{
    public partial class LPMEDDocument : UserControl,ICodeEditor
    {

        #region public properties
        /// <summary>
        /// Устройство которое описывает модуль в данном lpmed-файле
        /// </summary>
        public string Name { get; private set; }
        public string Path { get; private set; }
        public bool IsCompile { get; private set; }
        public string LPMEDCode
        {
            get
            {
                return Document.Text;
            }
        }
        public Scintilla Document
        {
            get
            {
                return EditControl;
            }
        }
        public bool Visible { get; private set; }
        public bool OnTabPage { get; private set; }
        #endregion

        public LPMEDDocument(string Path)
		{
            this.Path = Path;
            this.Name = System.IO.Path.GetFileName(Path);
			InitializeComponent();
		}

        private void sciDocument_SavePointReached(object sender, EventArgs e)
        {
            addOrRemoveAsteric();
        }

        private void sciDocument_SavePointLeft(object sender, EventArgs e)
        {
            addOrRemoveAsteric();
        }

        private void addOrRemoveAsteric()
        {

                if (this.Document.IsDirty)
                {
                    if (!this.Text.EndsWith(" *"))
                        this.Text += " *";
                }
                else
                {
                    if (this.Text.EndsWith(" *"))
                        this.Text = Text.Substring(0, this.Text.Length - 2);
                }
        }


		private bool save(string path)
		{
            using (FileStream fs = File.Create(path))
            using (StreamWriter bw = new StreamWriter(fs))
                bw.Write(EditControl.Text);//, 0, EditControl.RawText.Length - 1); // Omit trailing NULL

            EditControl.IsDirty = false;
			return true;
		}

        #region ICodeEditor Members

        public void Initialize(Control parent)
        {
            this.Dock = DockStyle.Fill;
            this.Parent = parent;
            parent.PerformLayout();
        }

        public ErrorReport Verify()
        {
            //TODO:
            //throw new NotImplementedException();
            return null;
        }

        public bool isDirty
        {
            get
            {
                //TODO:
                return true;
            }
        }

        public void SaveAs(string Path)
        {
            save(Path);
        }

        public void Save()
        {
            SaveAs(Path);
        }

        public void isCompile(bool val)
        {
            this.IsCompile = val;
        }

        public void Close()
        {
            OnTabPage = false;
            Visible = false;
        }

        public void OnSelect()
        {

            OnTabPage = true;
            Visible = true;
        }
        public void OnDeselect()
        {
            Visible = false;
        }

        #endregion
    }
}
